###############################################                                     
# Cause of Effect? Turnout in Hispanic Majority-Minority Districts
#  - John A. Henderson, Jasjeet S. Sekhon, and Rocio Titiunik
#  - Forthcoming in Political Analysis
#  - Replication file for <Table XV>
#  - April 14, 2016
###############################################

###########################################################################
#
## GENERATES TABLE XV: MAIN RESULTS FOR DIF-IN-DIF; MATCHING ON PPOP18; (N)HVAP in denominator, w/ overruns
#
# Use Huber-White SEs                                                    
#
###########################################################################

options(width=150)
rm(list=ls())   

path='/local/'  
source(paste(path,'replicationPA/funs/headers.R',sep=''))
    
set.seed(57459)


#WH2data <- read.dta(file = paste(path,"replicationPA/data/Baseline-afterMATCH-recreated.dta",sep=''))          
load(file = paste(path,"replicationPA/data/Baseline-afterMATCH-recreated.Rdata",sep=''))          
dim(WH2data) 

WH2data=WH2data[which(WH2data$dfound_pair_newdata==1),]  
dim(WH2data)
      
#Sdata <-  read.dta(file = paste(path,"replicationPA/data/Matched-recreated.dta",sep=''))
load(file = paste(path,"replicationPA/data/Matched-recreated.Rdata",sep=''))   
dim(Sdata)
      
Sdata=Sdata[which(Sdata$dfound_pair_newdata==1),]
dim(Sdata)


WH2data$hvap=WH2data$pop_hispanic18 
WH2data$nhvap=WH2data$vap-WH2data$hvap

WH2data$phh_income99_39less  = WH2data$phh_income99_0to19    + WH2data$phh_income99_20to39
WH2data$phh_income99_40to74  = WH2data$phh_income99_40to59   + WH2data$phh_income99_60to74
WH2data$phh_income99_100plus = WH2data$phh_income99_100to199 + WH2data$phh_income99_200
WH2data$ppop_foreign         = WH2data$ppop_foreign_naturcit + WH2data$ppop_foreign_nocit

WH2data$y98_reg_nhisp		 = WH2data$y98_reg_tot - WH2data$y98_reg_hisp 
WH2data$y98_preg_nhisp		 = WH2data$y98_reg_nhisp/WH2data$y98_reg_tot
       
WH2data$y00_turn_nhisp       = WH2data$y00_turn_tot - WH2data$y00_turn_hisp
WH2data$y00_reg_nhisp        = WH2data$y00_reg_tot - WH2data$y00_reg_hisp

WH2data$y02_turn_nhisp       = WH2data$y02_turn_tot - WH2data$y02_turn_hisp
WH2data$y02_reg_nhisp        = WH2data$y02_reg_tot  - WH2data$y02_reg_hisp                   

WH2data$y04_turn_nhisp       = WH2data$y04_turn_tot - WH2data$y04_turn_hisp
WH2data$y04_reg_nhisp        = WH2data$y04_reg_tot  - WH2data$y04_reg_hisp                                                                           
                                                                          
WH2data$y06_turn_nhisp       = WH2data$y06_turn_tot - WH2data$y06_turn_hisp
WH2data$y06_reg_nhisp        = WH2data$y06_reg_tot  - WH2data$y06_reg_hisp

Sdata$hvap=Sdata$pop_hispanic18 
Sdata$nhvap=Sdata$vap-Sdata$hvap  

Sdata$phh_income99_39less  = Sdata$phh_income99_0to19    + Sdata$phh_income99_20to39
Sdata$phh_income99_40to74  = Sdata$phh_income99_40to59   + Sdata$phh_income99_60to74
Sdata$phh_income99_100plus = Sdata$phh_income99_100to199 + Sdata$phh_income99_200
Sdata$ppop_foreign         = Sdata$ppop_foreign_naturcit + Sdata$ppop_foreign_nocit

Sdata$y98_reg_nhisp			 = Sdata$y98_reg_tot - Sdata$y98_reg_hisp
Sdata$y98_preg_nhisp		 = Sdata$y98_reg_nhisp/Sdata$y98_reg_tot

Sdata$y00_turn_nhisp       = Sdata$y00_turn_tot - Sdata$y00_turn_hisp
Sdata$y00_reg_nhisp        = Sdata$y00_reg_tot - Sdata$y00_reg_hisp

Sdata$y02_turn_nhisp       = Sdata$y02_turn_tot - Sdata$y02_turn_hisp
Sdata$y02_reg_nhisp        = Sdata$y02_reg_tot  - Sdata$y02_reg_hisp                   

Sdata$y04_turn_nhisp       = Sdata$y04_turn_tot - Sdata$y04_turn_hisp
Sdata$y04_reg_nhisp        = Sdata$y04_reg_tot  - Sdata$y04_reg_hisp                                                                           
                                                                          
Sdata$y06_turn_nhisp       = Sdata$y06_turn_tot - Sdata$y06_turn_hisp
Sdata$y06_reg_nhisp        = Sdata$y06_reg_tot  - Sdata$y06_reg_hisp         

names(Sdata)[which(names(Sdata)=='tr')]='Tr'
names(WH2data)[which(names(WH2data)=='tr')]='Tr'

    
# WH2data treatment 
Tr <- WH2data$Tr          
m1=c()
m1$index.treated=which(Tr==T)
m1$index.control=which(Tr==F)           
print(table(Tr)) 
    
# Sdata treatment 
Tr <- Sdata$Tr
m2=c()
m2$index.treated=which(Tr==T)
m2$index.control=which(Tr==F)           
print(table(Tr))
                          

######################################################
### Results matrix 
######################################################
ACTcolnm <- c("Mean Diff","Diff means p-val")

ACTrownm <- c(
"Hispanic Turnout 2004-2000 Difference WH2",
"Hispanic Registration 2004-2000 Difference WH2",
"NonHispanic Turnout 2004-2000 Difference WH2",
"NonHispanic Registration 2004-2000 Difference WH2",

"Hispanic Turnout 2004-2000 Difference AM",
"Hispanic Registration 2004-2000 Difference AM",
"NonHispanic Turnout 2004-2000 Difference AM",
"NonHispanic Registration 2004-2000 Difference AM"
)

RESACT=list()
RESACT[[1]] <- matrix(data=NA,nrow=length(ACTrownm), ncol=length(ACTcolnm), dimnames = list(ACTrownm,ACTcolnm))

ACTrownm <- c(
"Hispanic Turnout 2002-2000 Difference WH2",
"Hispanic Registration 2002-2000 Difference WH2",
"NonHispanic Turnout 2002-2000 Difference WH2",
"NonHispanic Registration 2002-2000 Difference WH2",


"Hispanic Turnout 2002-2000 Difference AM",
"Hispanic Registration 2002-2000 Difference AM",
"NonHispanic Turnout 2002-2000 Difference AM",
"NonHispanic Registration 2002-2000 Difference AM"
)

#RESACT=list()
RESACT[[2]] <- matrix(data=NA,nrow=length(ACTrownm), ncol=length(ACTcolnm), dimnames = list(ACTrownm,ACTcolnm))
          
ACTrownm <- c(
"Hispanic Turnout 2006-2000 Difference WH2",
"Hispanic Registration 2006-2000 Difference WH2",
"NonHispanic Turnout 2006-2000 Difference WH2",
"NonHispanic Registration 2006-2000 Difference WH2",


"Hispanic Turnout 2006-2000 Difference AM",
"Hispanic Registration 2006-2000 Difference AM",
"NonHispanic Turnout 2006-2000 Difference AM",
"NonHispanic Registration 2006-2000 Difference AM"
)

#RESACT=list()
RESACT[[3]] <- matrix(data=NA,nrow=length(ACTrownm), ncol=length(ACTcolnm), dimnames = list(ACTrownm,ACTcolnm))


######################################################
### Diff-in-Diff Results 
######################################################

######################################################
# A. 2004 - 2000
######################################################

######################################################
### (i) Diff-in-Diff results on Hispanic Registration
######################################################

#BASELINE
                                     
Y1 <- (WH2data$y04_reg_hisp/WH2data$hvap)
Y0 <- (WH2data$y00_reg_hisp/WH2data$hvap)      
          
Y1[which(WH2data$y04_reg_hisp==999999 | WH2data$hvap==0 | WH2data$hvap==999999)]<-NA
Y0[which(WH2data$y00_reg_hisp==999999 | WH2data$hvap==0 | WH2data$hvap==999999)]<-NA
       
print("Percent Overrun")  
print(rbind(c('2004','2000'),round(c(length(which(Y1>1))/length(Y1),length(which(Y0>1))/length(Y0)),digits=4)))
                         
#Y1[which(Y1>1)] <- NA
#Y0[which(Y0>1)] <- NA

Y1 <- Y1[c(m1$index.treated,m1$index.control)]
Y0 <- Y0[c(m1$index.treated,m1$index.control)]

Tr <- WH2data$Tr == 1
TrWH2<-Tr[c(m1$index.treated,m1$index.control)]


YWH2.1.Tr=Y1[TrWH2]
YWH2.1.Co=Y1[!TrWH2]
YWH2.0.Tr=Y0[TrWH2]
YWH2.0.Co=Y0[!TrWH2]

ddWH2.Tr <- mean(na.rm=T,YWH2.1.Tr) - mean(na.rm=T,YWH2.0.Tr)
ddWH2.Co <- mean(na.rm=T,YWH2.1.Co) - mean(na.rm=T,YWH2.0.Co)
ddWH2 <- ddWH2.Tr - ddWH2.Co 
print(ddWH2)

YY <- c(YWH2.1.Tr, YWH2.1.Co,YWH2.0.Tr, YWH2.0.Co)
time <- c(rep(1,length(c(YWH2.1.Tr, YWH2.1.Co))),rep(0,length(c(YWH2.0.Tr, YWH2.0.Co))))
TT <- c(rep(1,length(YWH2.1.Tr)),rep(0,length(YWH2.1.Co)),rep(1,length(YWH2.0.Tr)),rep(0,length(YWH2.0.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = YWH2.1.Tr - YWH2.0.Tr
DCo = YWH2.1.Co - YWH2.0.Co
D1 = YWH2.1.Tr - YWH2.1.Co  
D0 = YWH2.0.Tr - YWH2.0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

RESACT[[1]]["Hispanic Registration 2004-2000 Difference WH2","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[1]]["Hispanic Registration 2004-2000 Difference WH2","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]

     

######################################################
### (ii) Diff-in-Diff results on Hispanic Turnout
######################################################

#BASELINE
                                     
Y1 <- (WH2data$y04_turn_hisp/WH2data$hvap)
Y0 <- (WH2data$y00_turn_hisp/WH2data$hvap)       

Y1[which(WH2data$y04_turn_hisp==999999 | WH2data$hvap==0 | WH2data$hvap==999999)]<-NA
Y0[which(WH2data$y00_turn_hisp==999999 | WH2data$hvap==0 | WH2data$hvap==999999)]<-NA
  
print("Percent Overrun")  
print(rbind(c('2004','2000'),round(c(length(which(Y1>1))/length(Y1),length(which(Y0>1))/length(Y0)),digits=4)))
                         
#Y1[which(Y1>1)] <- NA
#Y0[which(Y0>1)] <- NA

Y1 <- Y1[c(m1$index.treated,m1$index.control)]
Y0 <- Y0[c(m1$index.treated,m1$index.control)]

Tr <- WH2data$Tr == 1    
TrWH2<-Tr[c(m1$index.treated,m1$index.control)]


YWH2.1.Tr=Y1[TrWH2]
YWH2.1.Co=Y1[!TrWH2]
YWH2.0.Tr=Y0[TrWH2]
YWH2.0.Co=Y0[!TrWH2]

ddWH2.Tr <- mean(na.rm=T,YWH2.1.Tr) - mean(na.rm=T,YWH2.0.Tr)
ddWH2.Co <- mean(na.rm=T,YWH2.1.Co) - mean(na.rm=T,YWH2.0.Co)
ddWH2 <- ddWH2.Tr - ddWH2.Co 
print(ddWH2)

YY <- c(YWH2.1.Tr, YWH2.1.Co,YWH2.0.Tr, YWH2.0.Co)
time <- c(rep(1,length(c(YWH2.1.Tr, YWH2.1.Co))),rep(0,length(c(YWH2.0.Tr, YWH2.0.Co))))
TT <- c(rep(1,length(YWH2.1.Tr)),rep(0,length(YWH2.1.Co)),rep(1,length(YWH2.0.Tr)),rep(0,length(YWH2.0.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = YWH2.1.Tr - YWH2.0.Tr
DCo = YWH2.1.Co - YWH2.0.Co
D1 = YWH2.1.Tr - YWH2.1.Co  
D0 = YWH2.0.Tr - YWH2.0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

RESACT[[1]]["Hispanic Turnout 2004-2000 Difference WH2","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[1]]["Hispanic Turnout 2004-2000 Difference WH2","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]


######################################################
### (iii) Diff-in-Diff results on NonHispanic Registration
######################################################

#BASELINE    

Y1 <- (WH2data$y04_reg_nhisp/WH2data$nhvap)
Y0 <- (WH2data$y00_reg_nhisp/WH2data$nhvap)    

Y1[which(WH2data$y04_reg_nhisp==999999 | WH2data$nhvap==0 | WH2data$nhvap==999999)]<-NA
Y0[which(WH2data$y00_reg_nhisp==999999 | WH2data$nhvap==0 | WH2data$nhvap==999999)]<-NA
   
print("Percent Overrun")  
print(rbind(c('2004','2000'),round(c(length(which(Y1>1))/length(Y1),length(which(Y0>1))/length(Y0)),digits=4)))
                         
#Y1[which(Y1>1)] <- NA
#Y0[which(Y0>1)] <- NA

Y1 <- Y1[c(m1$index.treated,m1$index.control)]
Y0 <- Y0[c(m1$index.treated,m1$index.control)]

Tr <- WH2data$Tr == 1    
TrWH2<-Tr[c(m1$index.treated,m1$index.control)]


YWH2.1.Tr=Y1[TrWH2]
YWH2.1.Co=Y1[!TrWH2]
YWH2.0.Tr=Y0[TrWH2]
YWH2.0.Co=Y0[!TrWH2]


ddWH2.Tr <- mean(na.rm=T,YWH2.1.Tr) - mean(na.rm=T,YWH2.0.Tr)
ddWH2.Co <- mean(na.rm=T,YWH2.1.Co) - mean(na.rm=T,YWH2.0.Co)
ddWH2 <- ddWH2.Tr - ddWH2.Co 
print(ddWH2)

YY <- c(YWH2.1.Tr, YWH2.1.Co,YWH2.0.Tr, YWH2.0.Co)
time <- c(rep(1,length(c(YWH2.1.Tr, YWH2.1.Co))),rep(0,length(c(YWH2.0.Tr, YWH2.0.Co))))
TT <- c(rep(1,length(YWH2.1.Tr)),rep(0,length(YWH2.1.Co)),rep(1,length(YWH2.0.Tr)),rep(0,length(YWH2.0.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = YWH2.1.Tr - YWH2.0.Tr
DCo = YWH2.1.Co - YWH2.0.Co
D1 = YWH2.1.Tr - YWH2.1.Co  
D0 = YWH2.0.Tr - YWH2.0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

RESACT[[1]]["NonHispanic Registration 2004-2000 Difference WH2","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[1]]["NonHispanic Registration 2004-2000 Difference WH2","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]



######################################################
### (iv) Diff-in-Diff results on NonHispanic Turnout
######################################################

#BASELINE

Y1 <- (WH2data$y04_turn_nhisp/WH2data$nhvap)
Y0 <- (WH2data$y00_turn_nhisp/WH2data$nhvap)
           
Y1[which(WH2data$y04_turn_nhisp==999999 | WH2data$nhvap==0 | WH2data$nhvap==999999)]<-NA
Y0[which(WH2data$y00_turn_nhisp==999999 | WH2data$nhvap==0 | WH2data$nhvap==999999)]<-NA
 
print("Percent Overrun")  
print(rbind(c('2004','2000'),round(c(length(which(Y1>1))/length(Y1),length(which(Y0>1))/length(Y0)),digits=4)))
                         
#Y1[which(Y1>1)] <- NA
#Y0[which(Y0>1)] <- NA

Y1 <- Y1[c(m1$index.treated,m1$index.control)]
Y0 <- Y0[c(m1$index.treated,m1$index.control)]

Tr <- WH2data$Tr == 1    
TrWH2<-Tr[c(m1$index.treated,m1$index.control)]


YWH2.1.Tr=Y1[TrWH2]
YWH2.1.Co=Y1[!TrWH2]
YWH2.0.Tr=Y0[TrWH2]
YWH2.0.Co=Y0[!TrWH2]


ddWH2.Tr <- mean(na.rm=T,YWH2.1.Tr) - mean(na.rm=T,YWH2.0.Tr)
ddWH2.Co <- mean(na.rm=T,YWH2.1.Co) - mean(na.rm=T,YWH2.0.Co)
ddWH2 <- ddWH2.Tr - ddWH2.Co 
print(ddWH2)

YY <- c(YWH2.1.Tr, YWH2.1.Co,YWH2.0.Tr, YWH2.0.Co)
time <- c(rep(1,length(c(YWH2.1.Tr, YWH2.1.Co))),rep(0,length(c(YWH2.0.Tr, YWH2.0.Co))))
TT <- c(rep(1,length(YWH2.1.Tr)),rep(0,length(YWH2.1.Co)),rep(1,length(YWH2.0.Tr)),rep(0,length(YWH2.0.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = YWH2.1.Tr - YWH2.0.Tr
DCo = YWH2.1.Co - YWH2.0.Co
D1 = YWH2.1.Tr - YWH2.1.Co  
D0 = YWH2.0.Tr - YWH2.0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

RESACT[[1]]["NonHispanic Turnout 2004-2000 Difference WH2","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[1]]["NonHispanic Turnout 2004-2000 Difference WH2","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]



######################################################
### (i) Diff-in-Diff results on Hispanic Registration
######################################################

#MATCHED 
    
YSt1 <- (Sdata$y04_reg_hisp/Sdata$hvap)
YSt0 <- (Sdata$y00_reg_hisp/Sdata$hvap)
   
indx <- which(Sdata$hvap==0 | Sdata$hvap==999999 | 
Sdata$y04_reg_hisp==999999 | Sdata$y00_reg_hisp==999999 )
  
YSt1[indx] <- NA
YSt0[indx] <- NA
  
print("Percent Overrun")  
print(rbind(c('2004','2000'),round(c(length(which(YSt1>1))/length(YSt1),length(which(YSt0>1))/length(YSt0)),digits=4)))
                         
#YSt1[which(YSt1>1)] <- NA
#YSt0[which(YSt0>1)] <- NA

Y1m.Tr<-YSt1[m2$index.treated]
Y1m.Co<-YSt1[m2$index.control]
Y1m  <- c(Y1m.Tr,Y1m.Co)
Tr1m <- c(rep(TRUE,length(Y1m.Tr)),rep(FALSE,length(Y1m.Co)))

Y0m.Tr<-YSt0[m2$index.treated]
Y0m.Co<-YSt0[m2$index.control]
Y0m  <- c(Y0m.Tr,Y0m.Co)
Tr1m <- c(rep(TRUE,length(Y0m.Tr)),rep(FALSE,length(Y0m.Co)))

mddTr <- mean(na.rm=T,Y1m.Tr) - mean(na.rm=T,Y0m.Tr)
mddCo <- mean(na.rm=T,Y1m.Co) - mean(na.rm=T,Y0m.Co)
mdd <- mddTr - mddCo
print(mdd)

YY <- c(Y1m.Tr, Y1m.Co,Y0m.Tr, Y0m.Co)
time <- c(rep(1,length(c(Y1m.Tr, Y1m.Co))),rep(0,length(c(Y0m.Tr, Y0m.Co))))
TT <- c(rep(1,length(Y1m.Tr)),rep(0,length(Y1m.Co)),rep(1,length(Y0m.Tr)),rep(0,length(Y0m.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = Y1m.Tr - Y0m.Tr
DCo = Y1m.Co - Y0m.Co
D1 = Y1m.Tr - Y1m.Co
D0 = Y0m.Tr - Y0m.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)


RESACT[[1]]["Hispanic Registration 2004-2000 Difference AM","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[1]]["Hispanic Registration 2004-2000 Difference AM","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]




######################################################
### (ii) Diff-in-Diff results on Hispanic Turnout
######################################################

#MATCHED

YSt1 <- (Sdata$y04_turn_hisp/Sdata$hvap)
YSt0 <- (Sdata$y00_turn_hisp/Sdata$hvap)
                
indx <- which(Sdata$hvap==0 | Sdata$hvap==999999 | 
Sdata$y04_turn_hisp==999999 | Sdata$y00_turn_hisp==999999 )

YSt1[indx] <- NA
YSt0[indx] <- NA
  
print("Percent Overrun")  
print(rbind(c('2004','2000'),round(c(length(which(YSt1>1))/length(YSt1),length(which(YSt0>1))/length(YSt0)),digits=4)))

#YSt1[which(YSt1>1)] <- NA
#YSt0[which(YSt0>1)] <- NA

Y1m.Tr<-YSt1[m2$index.treated]
Y1m.Co<-YSt1[m2$index.control]
Y1m  <- c(Y1m.Tr,Y1m.Co)
Tr1m <- c(rep(TRUE,length(Y1m.Tr)),rep(FALSE,length(Y1m.Co)))

Y0m.Tr<-YSt0[m2$index.treated]
Y0m.Co<-YSt0[m2$index.control]
Y0m  <- c(Y0m.Tr,Y0m.Co)
Tr1m <- c(rep(TRUE,length(Y0m.Tr)),rep(FALSE,length(Y0m.Co)))

mddTr <- mean(na.rm=T,Y1m.Tr) - mean(na.rm=T,Y0m.Tr)
mddCo <- mean(na.rm=T,Y1m.Co) - mean(na.rm=T,Y0m.Co)
mdd <- mddTr - mddCo
print(mdd)

YY <- c(Y1m.Tr, Y1m.Co,Y0m.Tr, Y0m.Co)
time <- c(rep(1,length(c(Y1m.Tr, Y1m.Co))),rep(0,length(c(Y0m.Tr, Y0m.Co))))
TT <- c(rep(1,length(Y1m.Tr)),rep(0,length(Y1m.Co)),rep(1,length(Y0m.Tr)),rep(0,length(Y0m.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = Y1m.Tr - Y0m.Tr
DCo = Y1m.Co - Y0m.Co
D1 = Y1m.Tr - Y1m.Co
D0 = Y0m.Tr - Y0m.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)


RESACT[[1]]["Hispanic Turnout 2004-2000 Difference AM","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[1]]["Hispanic Turnout 2004-2000 Difference AM","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]


######################################################
### (iii) Diff-in-Diff results on NonHispanic Registration
######################################################

#MATCHED  
   

YSt1 <- (Sdata$y04_reg_nhisp/Sdata$nhvap)
YSt0 <- (Sdata$y00_reg_nhisp/Sdata$nhvap)
      
indx <- which(Sdata$nhvap==0 | Sdata$nhvap==999999 |
Sdata$y04_reg_nhisp==999999 | Sdata$y00_reg_nhisp==999999 )

YSt1[indx] <- NA
YSt0[indx] <- NA
 
print("Percent Overrun")  
print(rbind(c('2004','2000'),round(c(length(which(YSt1>1))/length(YSt1),length(which(YSt0>1))/length(YSt0)),digits=4)))

#YSt1[which(YSt1>1)] <- NA
#YSt0[which(YSt0>1)] <- NA

Y1m.Tr<-YSt1[m2$index.treated]
Y1m.Co<-YSt1[m2$index.control]
Y1m  <- c(Y1m.Tr,Y1m.Co)
Tr1m <- c(rep(TRUE,length(Y1m.Tr)),rep(FALSE,length(Y1m.Co)))

Y0m.Tr<-YSt0[m2$index.treated]
Y0m.Co<-YSt0[m2$index.control]
Y0m  <- c(Y0m.Tr,Y0m.Co)
Tr1m <- c(rep(TRUE,length(Y0m.Tr)),rep(FALSE,length(Y0m.Co)))

mddTr <- mean(na.rm=T,Y1m.Tr) - mean(na.rm=T,Y0m.Tr)
mddCo <- mean(na.rm=T,Y1m.Co) - mean(na.rm=T,Y0m.Co)
mdd <- mddTr - mddCo
print(mdd)

YY <- c(Y1m.Tr, Y1m.Co,Y0m.Tr, Y0m.Co)
time <- c(rep(1,length(c(Y1m.Tr, Y1m.Co))),rep(0,length(c(Y0m.Tr, Y0m.Co))))
TT <- c(rep(1,length(Y1m.Tr)),rep(0,length(Y1m.Co)),rep(1,length(Y0m.Tr)),rep(0,length(Y0m.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = Y1m.Tr - Y0m.Tr
DCo = Y1m.Co - Y0m.Co
D1 = Y1m.Tr - Y1m.Co
D0 = Y0m.Tr - Y0m.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)


RESACT[[1]]["NonHispanic Registration 2004-2000 Difference AM","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[1]]["NonHispanic Registration 2004-2000 Difference AM","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]


######################################################
### (iv) Diff-in-Diff results on NonHispanic Turnout
######################################################

#MATCHED

YSt1 <- (Sdata$y04_turn_nhisp/Sdata$nhvap)
YSt0 <- (Sdata$y00_turn_nhisp/Sdata$nhvap)

indx <- which(Sdata$nhvap==0 | Sdata$nhvap==999999 |
Sdata$y04_turn_nhisp==999999 | Sdata$y00_turn_nhisp==999999 )

YSt1[indx] <- NA
YSt0[indx] <- NA
  
print("Percent Overrun")  
print(rbind(c('2004','2000'),round(c(length(which(YSt1>1))/length(YSt1),length(which(YSt0>1))/length(YSt0)),digits=4)))

#YSt1[which(YSt1>1)] <- NA
#YSt0[which(YSt0>1)] <- NA

Y1m.Tr<-YSt1[m2$index.treated]
Y1m.Co<-YSt1[m2$index.control]
Y1m  <- c(Y1m.Tr,Y1m.Co)
Tr1m <- c(rep(TRUE,length(Y1m.Tr)),rep(FALSE,length(Y1m.Co)))

Y0m.Tr<-YSt0[m2$index.treated]
Y0m.Co<-YSt0[m2$index.control]
Y0m  <- c(Y0m.Tr,Y0m.Co)
Tr1m <- c(rep(TRUE,length(Y0m.Tr)),rep(FALSE,length(Y0m.Co)))

mddTr <- mean(na.rm=T,Y1m.Tr) - mean(na.rm=T,Y0m.Tr)
mddCo <- mean(na.rm=T,Y1m.Co) - mean(na.rm=T,Y0m.Co)
mdd <- mddTr - mddCo
print(mdd)

YY <- c(Y1m.Tr, Y1m.Co,Y0m.Tr, Y0m.Co)
time <- c(rep(1,length(c(Y1m.Tr, Y1m.Co))),rep(0,length(c(Y0m.Tr, Y0m.Co))))
TT <- c(rep(1,length(Y1m.Tr)),rep(0,length(Y1m.Co)),rep(1,length(Y0m.Tr)),rep(0,length(Y0m.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = Y1m.Tr - Y0m.Tr
DCo = Y1m.Co - Y0m.Co
D1 = Y1m.Tr - Y1m.Co
D0 = Y0m.Tr - Y0m.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)


RESACT[[1]]["NonHispanic Turnout 2004-2000 Difference AM","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[1]]["NonHispanic Turnout 2004-2000 Difference AM","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]



######################################################
### Diff-in-Diff Results 
######################################################

######################################################
# A. 2002 - 2000
######################################################

######################################################
### (i) Diff-in-Diff results on Hispanic Registration
######################################################


#BASELINE
                                     
Y1 <- (WH2data$y02_reg_hisp/WH2data$hvap)
Y0 <- (WH2data$y00_reg_hisp/WH2data$hvap)
              
Y1[which(WH2data$y02_reg_hisp==999999 | WH2data$hvap==0 | WH2data$hvap==999999)]<-NA
Y0[which(WH2data$y00_reg_hisp==999999 | WH2data$hvap==0 | WH2data$hvap==999999)]<-NA
              
print("Percent Overrun")  
print(rbind(c('2002','2000'),round(c(length(which(Y1>1))/length(Y1),length(which(Y0>1))/length(Y0)),digits=4)))
                         
#Y1[which(Y1>1)] <- NA
#Y0[which(Y0>1)] <- NA

Y1 <- Y1[c(m1$index.treated,m1$index.control)]
Y0 <- Y0[c(m1$index.treated,m1$index.control)]

Tr <- WH2data$Tr == 1    
TrWH2<-Tr[c(m1$index.treated,m1$index.control)]


YWH2.1.Tr=Y1[TrWH2]
YWH2.1.Co=Y1[!TrWH2]
YWH2.0.Tr=Y0[TrWH2]
YWH2.0.Co=Y0[!TrWH2]

ddWH2.Tr <- mean(na.rm=T,YWH2.1.Tr) - mean(na.rm=T,YWH2.0.Tr)
ddWH2.Co <- mean(na.rm=T,YWH2.1.Co) - mean(na.rm=T,YWH2.0.Co)
ddWH2 <- ddWH2.Tr - ddWH2.Co 
print(ddWH2)

YY <- c(YWH2.1.Tr, YWH2.1.Co,YWH2.0.Tr, YWH2.0.Co)
time <- c(rep(1,length(c(YWH2.1.Tr, YWH2.1.Co))),rep(0,length(c(YWH2.0.Tr, YWH2.0.Co))))
TT <- c(rep(1,length(YWH2.1.Tr)),rep(0,length(YWH2.1.Co)),rep(1,length(YWH2.0.Tr)),rep(0,length(YWH2.0.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = YWH2.1.Tr - YWH2.0.Tr
DCo = YWH2.1.Co - YWH2.0.Co
D1 = YWH2.1.Tr - YWH2.1.Co  
D0 = YWH2.0.Tr - YWH2.0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

RESACT[[2]]["Hispanic Registration 2002-2000 Difference WH2","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[2]]["Hispanic Registration 2002-2000 Difference WH2","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]

     

######################################################
### (ii) Diff-in-Diff results on Hispanic Turnout
######################################################

#BASELINE
                                     
Y1 <- (WH2data$y02_turn_hisp/WH2data$hvap)
Y0 <- (WH2data$y00_turn_hisp/WH2data$hvap)
               
Y1[which(WH2data$y02_turn_hisp==999999 | WH2data$hvap==0 | WH2data$hvap==999999)]<-NA
Y0[which(WH2data$y00_turn_hisp==999999 | WH2data$hvap==0 | WH2data$hvap==999999)]<-NA
   
print("Percent Overrun")  
print(rbind(c('2002','2000'),round(c(length(which(Y1>1))/length(Y1),length(which(Y0>1))/length(Y0)),digits=4)))
                         
#Y1[which(Y1>1)] <- NA
#Y0[which(Y0>1)] <- NA

Y1 <- Y1[c(m1$index.treated,m1$index.control)]
Y0 <- Y0[c(m1$index.treated,m1$index.control)]

Tr <- WH2data$Tr == 1    
TrWH2<-Tr[c(m1$index.treated,m1$index.control)]


YWH2.1.Tr=Y1[TrWH2]
YWH2.1.Co=Y1[!TrWH2]
YWH2.0.Tr=Y0[TrWH2]
YWH2.0.Co=Y0[!TrWH2]

ddWH2.Tr <- mean(na.rm=T,YWH2.1.Tr) - mean(na.rm=T,YWH2.0.Tr)
ddWH2.Co <- mean(na.rm=T,YWH2.1.Co) - mean(na.rm=T,YWH2.0.Co)
ddWH2 <- ddWH2.Tr - ddWH2.Co 
print(ddWH2)

YY <- c(YWH2.1.Tr, YWH2.1.Co,YWH2.0.Tr, YWH2.0.Co)
time <- c(rep(1,length(c(YWH2.1.Tr, YWH2.1.Co))),rep(0,length(c(YWH2.0.Tr, YWH2.0.Co))))
TT <- c(rep(1,length(YWH2.1.Tr)),rep(0,length(YWH2.1.Co)),rep(1,length(YWH2.0.Tr)),rep(0,length(YWH2.0.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = YWH2.1.Tr - YWH2.0.Tr
DCo = YWH2.1.Co - YWH2.0.Co
D1 = YWH2.1.Tr - YWH2.1.Co  
D0 = YWH2.0.Tr - YWH2.0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

RESACT[[2]]["Hispanic Turnout 2002-2000 Difference WH2","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[2]]["Hispanic Turnout 2002-2000 Difference WH2","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]


######################################################
### (iii) Diff-in-Diff results on NonHispanic Registration
######################################################

#BASELINE    

Y1 <- (WH2data$y02_reg_nhisp/WH2data$nhvap)
Y0 <- (WH2data$y00_reg_nhisp/WH2data$nhvap)

Y1[which(WH2data$y02_reg_nhisp==999999 | WH2data$nhvap==0 | WH2data$nhvap==999999)]<-NA
Y0[which(WH2data$y00_reg_nhisp==999999 | WH2data$nhvap==0 | WH2data$nhvap==999999)]<-NA

print("Percent Overrun")  
print(rbind(c('2002','2000'),round(c(length(which(Y1>1))/length(Y1),length(which(Y0>1))/length(Y0)),digits=4)))

#Y1[which(Y1>1)] <- NA
#Y0[which(Y0>1)] <- NA
		
Y1 <- Y1[c(m1$index.treated,m1$index.control)]
Y0 <- Y0[c(m1$index.treated,m1$index.control)]

Tr <- WH2data$Tr == 1    
TrWH2<-Tr[c(m1$index.treated,m1$index.control)]


YWH2.1.Tr=Y1[TrWH2]
YWH2.1.Co=Y1[!TrWH2]
YWH2.0.Tr=Y0[TrWH2]
YWH2.0.Co=Y0[!TrWH2]


ddWH2.Tr <- mean(na.rm=T,YWH2.1.Tr) - mean(na.rm=T,YWH2.0.Tr)
ddWH2.Co <- mean(na.rm=T,YWH2.1.Co) - mean(na.rm=T,YWH2.0.Co)
ddWH2 <- ddWH2.Tr - ddWH2.Co 
print(ddWH2)

YY <- c(YWH2.1.Tr, YWH2.1.Co,YWH2.0.Tr, YWH2.0.Co)
time <- c(rep(1,length(c(YWH2.1.Tr, YWH2.1.Co))),rep(0,length(c(YWH2.0.Tr, YWH2.0.Co))))
TT <- c(rep(1,length(YWH2.1.Tr)),rep(0,length(YWH2.1.Co)),rep(1,length(YWH2.0.Tr)),rep(0,length(YWH2.0.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = YWH2.1.Tr - YWH2.0.Tr
DCo = YWH2.1.Co - YWH2.0.Co
D1 = YWH2.1.Tr - YWH2.1.Co  
D0 = YWH2.0.Tr - YWH2.0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

RESACT[[2]]["NonHispanic Registration 2002-2000 Difference WH2","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[2]]["NonHispanic Registration 2002-2000 Difference WH2","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]



######################################################
### (iv) Diff-in-Diff results on NonHispanic Turnout
######################################################

#BASELINE

Y1 <- (WH2data$y02_turn_nhisp/WH2data$nhvap)
Y0 <- (WH2data$y00_turn_nhisp/WH2data$nhvap)
     
Y1[which(WH2data$y02_turn_nhisp==999999 | WH2data$nhvap==0 | WH2data$nhvap==999999)]<-NA
Y0[which(WH2data$y00_turn_nhisp==999999 | WH2data$nhvap==0 | WH2data$nhvap==999999)]<-NA

print("Percent Overrun")  
print(rbind(c('2002','2000'),round(c(length(which(Y1>1))/length(Y1),length(which(Y0>1))/length(Y0)),digits=4)))
                         
#Y1[which(Y1>1)] <- NA
#Y0[which(Y0>1)] <- NA
                   
Y1 <- Y1[c(m1$index.treated,m1$index.control)]
Y0 <- Y0[c(m1$index.treated,m1$index.control)]

Tr <- WH2data$Tr == 1    
TrWH2<-Tr[c(m1$index.treated,m1$index.control)]


YWH2.1.Tr=Y1[TrWH2]
YWH2.1.Co=Y1[!TrWH2]
YWH2.0.Tr=Y0[TrWH2]
YWH2.0.Co=Y0[!TrWH2]


ddWH2.Tr <- mean(na.rm=T,YWH2.1.Tr) - mean(na.rm=T,YWH2.0.Tr)
ddWH2.Co <- mean(na.rm=T,YWH2.1.Co) - mean(na.rm=T,YWH2.0.Co)
ddWH2 <- ddWH2.Tr - ddWH2.Co 
print(ddWH2)

YY <- c(YWH2.1.Tr, YWH2.1.Co,YWH2.0.Tr, YWH2.0.Co)
time <- c(rep(1,length(c(YWH2.1.Tr, YWH2.1.Co))),rep(0,length(c(YWH2.0.Tr, YWH2.0.Co))))
TT <- c(rep(1,length(YWH2.1.Tr)),rep(0,length(YWH2.1.Co)),rep(1,length(YWH2.0.Tr)),rep(0,length(YWH2.0.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = YWH2.1.Tr - YWH2.0.Tr
DCo = YWH2.1.Co - YWH2.0.Co
D1 = YWH2.1.Tr - YWH2.1.Co  
D0 = YWH2.0.Tr - YWH2.0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

RESACT[[2]]["NonHispanic Turnout 2002-2000 Difference WH2","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[2]]["NonHispanic Turnout 2002-2000 Difference WH2","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]



######################################################
### (i) Diff-in-Diff results on Hispanic Registration
######################################################

#MATCHED 
    
YSt1 <- (Sdata$y02_reg_hisp/Sdata$hvap)
YSt0 <- (Sdata$y00_reg_hisp/Sdata$hvap)

indx <- which(Sdata$hvap==0 | Sdata$hvap==999999 | 
Sdata$y02_reg_hisp==999999 | Sdata$y00_reg_hisp==999999 )

YSt1[indx] <- NA
YSt0[indx] <- NA

print("Percent Overrun")  
print(rbind(c('2002','2000'),round(c(length(which(YSt1>1))/length(YSt1),length(which(YSt0>1))/length(YSt0)),digits=4)))

#YSt1[which(YSt1>1)] <- NA
#YSt0[which(YSt0>1)] <- NA          

Y1m.Tr<-YSt1[m2$index.treated]
Y1m.Co<-YSt1[m2$index.control]
Y1m  <- c(Y1m.Tr,Y1m.Co)
Tr1m <- c(rep(TRUE,length(Y1m.Tr)),rep(FALSE,length(Y1m.Co)))

Y0m.Tr<-YSt0[m2$index.treated]
Y0m.Co<-YSt0[m2$index.control]
Y0m  <- c(Y0m.Tr,Y0m.Co)
Tr1m <- c(rep(TRUE,length(Y0m.Tr)),rep(FALSE,length(Y0m.Co)))

mddTr <- mean(na.rm=T,Y1m.Tr) - mean(na.rm=T,Y0m.Tr)
mddCo <- mean(na.rm=T,Y1m.Co) - mean(na.rm=T,Y0m.Co)
mdd <- mddTr - mddCo
print(mdd)

YY <- c(Y1m.Tr, Y1m.Co,Y0m.Tr, Y0m.Co)
time <- c(rep(1,length(c(Y1m.Tr, Y1m.Co))),rep(0,length(c(Y0m.Tr, Y0m.Co))))
TT <- c(rep(1,length(Y1m.Tr)),rep(0,length(Y1m.Co)),rep(1,length(Y0m.Tr)),rep(0,length(Y0m.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = Y1m.Tr - Y0m.Tr
DCo = Y1m.Co - Y0m.Co
D1 = Y1m.Tr - Y1m.Co
D0 = Y0m.Tr - Y0m.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)


RESACT[[2]]["Hispanic Registration 2002-2000 Difference AM","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[2]]["Hispanic Registration 2002-2000 Difference AM","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]




######################################################
### (ii) Diff-in-Diff results on Hispanic Turnout
######################################################

#MATCHED

YSt1 <- (Sdata$y02_turn_hisp/Sdata$hvap)
YSt0 <- (Sdata$y00_turn_hisp/Sdata$hvap)

indx <- which(Sdata$hvap==0 | Sdata$hvap==999999 | 
Sdata$y02_turn_hisp==999999 | Sdata$y00_turn_hisp==999999 )

YSt1[indx] <- NA
YSt0[indx] <- NA
      
print("Percent Overrun")  
print(rbind(c('2002','2000'),round(c(length(which(YSt1>1))/length(YSt1),length(which(YSt0>1))/length(YSt0)),digits=4)))

#YSt1[which(YSt1>1)] <- NA
#YSt0[which(YSt0>1)] <- NA

Y1m.Tr<-YSt1[m2$index.treated]
Y1m.Co<-YSt1[m2$index.control]
Y1m  <- c(Y1m.Tr,Y1m.Co)
Tr1m <- c(rep(TRUE,length(Y1m.Tr)),rep(FALSE,length(Y1m.Co)))

Y0m.Tr<-YSt0[m2$index.treated]
Y0m.Co<-YSt0[m2$index.control]
Y0m  <- c(Y0m.Tr,Y0m.Co)
Tr1m <- c(rep(TRUE,length(Y0m.Tr)),rep(FALSE,length(Y0m.Co)))

mddTr <- mean(na.rm=T,Y1m.Tr) - mean(na.rm=T,Y0m.Tr)
mddCo <- mean(na.rm=T,Y1m.Co) - mean(na.rm=T,Y0m.Co)
mdd <- mddTr - mddCo
print(mdd)

YY <- c(Y1m.Tr, Y1m.Co,Y0m.Tr, Y0m.Co)
time <- c(rep(1,length(c(Y1m.Tr, Y1m.Co))),rep(0,length(c(Y0m.Tr, Y0m.Co))))
TT <- c(rep(1,length(Y1m.Tr)),rep(0,length(Y1m.Co)),rep(1,length(Y0m.Tr)),rep(0,length(Y0m.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = Y1m.Tr - Y0m.Tr
DCo = Y1m.Co - Y0m.Co
D1 = Y1m.Tr - Y1m.Co
D0 = Y0m.Tr - Y0m.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)


RESACT[[2]]["Hispanic Turnout 2002-2000 Difference AM","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[2]]["Hispanic Turnout 2002-2000 Difference AM","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]


######################################################
### (iii) Diff-in-Diff results on NonHispanic Registration
######################################################

#MATCHED  
   

YSt1 <- (Sdata$y02_reg_nhisp/Sdata$nhvap)
YSt0 <- (Sdata$y00_reg_nhisp/Sdata$nhvap)

indx <- which(Sdata$nhvap==0 | Sdata$nhvap==999999 |
Sdata$y02_reg_nhisp==999999 | Sdata$y00_reg_nhisp==999999 )

YSt1[indx] <- NA
YSt0[indx] <- NA
   
print("Percent Overrun")  
print(rbind(c('2002','2000'),round(c(length(which(YSt1>1))/length(YSt1),length(which(YSt0>1))/length(YSt0)),digits=4)))

#YSt1[which(YSt1>1)] <- NA
#YSt0[which(YSt0>1)] <- NA

Y1m.Tr<-YSt1[m2$index.treated]
Y1m.Co<-YSt1[m2$index.control]
Y1m  <- c(Y1m.Tr,Y1m.Co)
Tr1m <- c(rep(TRUE,length(Y1m.Tr)),rep(FALSE,length(Y1m.Co)))

Y0m.Tr<-YSt0[m2$index.treated]
Y0m.Co<-YSt0[m2$index.control]
Y0m  <- c(Y0m.Tr,Y0m.Co)
Tr1m <- c(rep(TRUE,length(Y0m.Tr)),rep(FALSE,length(Y0m.Co)))

mddTr <- mean(na.rm=T,Y1m.Tr) - mean(na.rm=T,Y0m.Tr)
mddCo <- mean(na.rm=T,Y1m.Co) - mean(na.rm=T,Y0m.Co)
mdd <- mddTr - mddCo
print(mdd)

YY <- c(Y1m.Tr, Y1m.Co,Y0m.Tr, Y0m.Co)
time <- c(rep(1,length(c(Y1m.Tr, Y1m.Co))),rep(0,length(c(Y0m.Tr, Y0m.Co))))
TT <- c(rep(1,length(Y1m.Tr)),rep(0,length(Y1m.Co)),rep(1,length(Y0m.Tr)),rep(0,length(Y0m.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = Y1m.Tr - Y0m.Tr
DCo = Y1m.Co - Y0m.Co
D1 = Y1m.Tr - Y1m.Co
D0 = Y0m.Tr - Y0m.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)


RESACT[[2]]["NonHispanic Registration 2002-2000 Difference AM","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[2]]["NonHispanic Registration 2002-2000 Difference AM","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]


######################################################
### (iv) Diff-in-Diff results on NonHispanic Turnout
######################################################

#MATCHED

YSt1 <- (Sdata$y02_turn_nhisp/Sdata$nhvap)
YSt0 <- (Sdata$y00_turn_nhisp/Sdata$nhvap)

indx <- which(Sdata$nhvap==0 | Sdata$nhvap==999999 |
Sdata$y02_turn_nhisp==999999 | Sdata$y00_turn_nhisp==999999 )

YSt1[indx] <- NA
YSt0[indx] <- NA

print("Percent Overrun")  
print(rbind(c('2002','2000'),round(c(length(which(YSt1>1))/length(YSt1),length(which(YSt0>1))/length(YSt0)),digits=4)))

#YSt1[which(YSt1>1)] <- NA
#YSt0[which(YSt0>1)] <- NA

Y1m.Tr<-YSt1[m2$index.treated]
Y1m.Co<-YSt1[m2$index.control]
Y1m  <- c(Y1m.Tr,Y1m.Co)
Tr1m <- c(rep(TRUE,length(Y1m.Tr)),rep(FALSE,length(Y1m.Co)))

Y0m.Tr<-YSt0[m2$index.treated]
Y0m.Co<-YSt0[m2$index.control]
Y0m  <- c(Y0m.Tr,Y0m.Co)
Tr1m <- c(rep(TRUE,length(Y0m.Tr)),rep(FALSE,length(Y0m.Co)))

mddTr <- mean(na.rm=T,Y1m.Tr) - mean(na.rm=T,Y0m.Tr)
mddCo <- mean(na.rm=T,Y1m.Co) - mean(na.rm=T,Y0m.Co)
mdd <- mddTr - mddCo
print(mdd)

YY <- c(Y1m.Tr, Y1m.Co,Y0m.Tr, Y0m.Co)
time <- c(rep(1,length(c(Y1m.Tr, Y1m.Co))),rep(0,length(c(Y0m.Tr, Y0m.Co))))
TT <- c(rep(1,length(Y1m.Tr)),rep(0,length(Y1m.Co)),rep(1,length(Y0m.Tr)),rep(0,length(Y0m.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = Y1m.Tr - Y0m.Tr
DCo = Y1m.Co - Y0m.Co
D1 = Y1m.Tr - Y1m.Co
D0 = Y0m.Tr - Y0m.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)


RESACT[[2]]["NonHispanic Turnout 2002-2000 Difference AM","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[2]]["NonHispanic Turnout 2002-2000 Difference AM","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]



######################################################
### Diff-in-Diff Results 
######################################################

######################################################
# A. 2006 - 2000
######################################################

######################################################
### (i) Diff-in-Diff results on Hispanic Registration
######################################################


#BASELINE
                                     
Y1 <- (WH2data$y06_reg_hisp/WH2data$hvap)
Y0 <- (WH2data$y00_reg_hisp/WH2data$hvap)

Y1[which(WH2data$y06_reg_hisp==999999 | WH2data$hvap==0 | WH2data$hvap==999999)]<-NA
Y0[which(WH2data$y00_reg_hisp==999999 | WH2data$hvap==0 | WH2data$hvap==999999)]<-NA
        
print("Percent Overrun")  
print(rbind(c('2006','2000'),round(c(length(which(Y1>1))/length(Y1),length(which(Y0>1))/length(Y0)),digits=4)))
                         
#Y1[which(Y1>1)] <- NA
#Y0[which(Y0>1)] <- NA

Y1 <- Y1[c(m1$index.treated,m1$index.control)]
Y0 <- Y0[c(m1$index.treated,m1$index.control)]

Tr <- WH2data$Tr == 1    
TrWH2<-Tr[c(m1$index.treated,m1$index.control)]


YWH2.1.Tr=Y1[TrWH2]
YWH2.1.Co=Y1[!TrWH2]
YWH2.0.Tr=Y0[TrWH2]
YWH2.0.Co=Y0[!TrWH2]

ddWH2.Tr <- mean(na.rm=T,YWH2.1.Tr) - mean(na.rm=T,YWH2.0.Tr)
ddWH2.Co <- mean(na.rm=T,YWH2.1.Co) - mean(na.rm=T,YWH2.0.Co)
ddWH2 <- ddWH2.Tr - ddWH2.Co 
print(ddWH2)

YY <- c(YWH2.1.Tr, YWH2.1.Co,YWH2.0.Tr, YWH2.0.Co)
time <- c(rep(1,length(c(YWH2.1.Tr, YWH2.1.Co))),rep(0,length(c(YWH2.0.Tr, YWH2.0.Co))))
TT <- c(rep(1,length(YWH2.1.Tr)),rep(0,length(YWH2.1.Co)),rep(1,length(YWH2.0.Tr)),rep(0,length(YWH2.0.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = YWH2.1.Tr - YWH2.0.Tr
DCo = YWH2.1.Co - YWH2.0.Co
D1 = YWH2.1.Tr - YWH2.1.Co  
D0 = YWH2.0.Tr - YWH2.0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

RESACT[[3]]["Hispanic Registration 2006-2000 Difference WH2","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[3]]["Hispanic Registration 2006-2000 Difference WH2","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]

     

######################################################
### (ii) Diff-in-Diff results on Hispanic Turnout
######################################################

#BASELINE
                                     
Y1 <- (WH2data$y06_turn_hisp/WH2data$hvap)
Y0 <- (WH2data$y00_turn_hisp/WH2data$hvap)

Y1[which(WH2data$y06_turn_hisp==999999 | WH2data$hvap==0 | WH2data$hvap==999999)]<-NA
Y0[which(WH2data$y00_turn_hisp==999999 | WH2data$hvap==0 | WH2data$hvap==999999)]<-NA
   
print("Percent Overrun")  
print(rbind(c('2006','2000'),round(c(length(which(Y1>1))/length(Y1),length(which(Y0>1))/length(Y0)),digits=4)))
                         
#Y1[which(Y1>1)] <- NA
#Y0[which(Y0>1)] <- NA

Y1 <- Y1[c(m1$index.treated,m1$index.control)]
Y0 <- Y0[c(m1$index.treated,m1$index.control)]

Tr <- WH2data$Tr == 1    
TrWH2<-Tr[c(m1$index.treated,m1$index.control)]


YWH2.1.Tr=Y1[TrWH2]
YWH2.1.Co=Y1[!TrWH2]
YWH2.0.Tr=Y0[TrWH2]
YWH2.0.Co=Y0[!TrWH2]

ddWH2.Tr <- mean(na.rm=T,YWH2.1.Tr) - mean(na.rm=T,YWH2.0.Tr)
ddWH2.Co <- mean(na.rm=T,YWH2.1.Co) - mean(na.rm=T,YWH2.0.Co)
ddWH2 <- ddWH2.Tr - ddWH2.Co 
print(ddWH2)

YY <- c(YWH2.1.Tr, YWH2.1.Co,YWH2.0.Tr, YWH2.0.Co)
time <- c(rep(1,length(c(YWH2.1.Tr, YWH2.1.Co))),rep(0,length(c(YWH2.0.Tr, YWH2.0.Co))))
TT <- c(rep(1,length(YWH2.1.Tr)),rep(0,length(YWH2.1.Co)),rep(1,length(YWH2.0.Tr)),rep(0,length(YWH2.0.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = YWH2.1.Tr - YWH2.0.Tr
DCo = YWH2.1.Co - YWH2.0.Co
D1 = YWH2.1.Tr - YWH2.1.Co  
D0 = YWH2.0.Tr - YWH2.0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

RESACT[[3]]["Hispanic Turnout 2006-2000 Difference WH2","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[3]]["Hispanic Turnout 2006-2000 Difference WH2","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]


######################################################
### (iii) Diff-in-Diff results on NonHispanic Registration
######################################################

#BASELINE    

Y1 <- (WH2data$y06_reg_nhisp/WH2data$nhvap)
Y0 <- (WH2data$y00_reg_nhisp/WH2data$nhvap)

Y1[which(WH2data$y06_reg_nhisp==999999 | WH2data$nhvap==0 | WH2data$nhvap==999999)]<-NA
Y0[which(WH2data$y00_reg_nhisp==999999 | WH2data$nhvap==0 | WH2data$nhvap==999999)]<-NA
  
print("Percent Overrun")  
print(rbind(c('2006','2000'),round(c(length(which(Y1>1))/length(Y1),length(which(Y0>1))/length(Y0)),digits=4)))
                         
#Y1[which(Y1>1)] <- NA
#Y0[which(Y0>1)] <- NA

Y1 <- Y1[c(m1$index.treated,m1$index.control)]
Y0 <- Y0[c(m1$index.treated,m1$index.control)]

Tr <- WH2data$Tr == 1    
TrWH2<-Tr[c(m1$index.treated,m1$index.control)]


YWH2.1.Tr=Y1[TrWH2]
YWH2.1.Co=Y1[!TrWH2]
YWH2.0.Tr=Y0[TrWH2]
YWH2.0.Co=Y0[!TrWH2]


ddWH2.Tr <- mean(na.rm=T,YWH2.1.Tr) - mean(na.rm=T,YWH2.0.Tr)
ddWH2.Co <- mean(na.rm=T,YWH2.1.Co) - mean(na.rm=T,YWH2.0.Co)
ddWH2 <- ddWH2.Tr - ddWH2.Co 
print(ddWH2)

YY <- c(YWH2.1.Tr, YWH2.1.Co,YWH2.0.Tr, YWH2.0.Co)
time <- c(rep(1,length(c(YWH2.1.Tr, YWH2.1.Co))),rep(0,length(c(YWH2.0.Tr, YWH2.0.Co))))
TT <- c(rep(1,length(YWH2.1.Tr)),rep(0,length(YWH2.1.Co)),rep(1,length(YWH2.0.Tr)),rep(0,length(YWH2.0.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = YWH2.1.Tr - YWH2.0.Tr
DCo = YWH2.1.Co - YWH2.0.Co
D1 = YWH2.1.Tr - YWH2.1.Co  
D0 = YWH2.0.Tr - YWH2.0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

RESACT[[3]]["NonHispanic Registration 2006-2000 Difference WH2","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[3]]["NonHispanic Registration 2006-2000 Difference WH2","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]



######################################################
### (iv) Diff-in-Diff results on NonHispanic Turnout
######################################################

#BASELINE

Y1 <- (WH2data$y06_turn_nhisp/WH2data$nhvap)
Y0 <- (WH2data$y00_turn_nhisp/WH2data$nhvap)

Y1[which(WH2data$y06_turn_nhisp==999999 | WH2data$nhvap==0 | WH2data$nhvap==999999)]<-NA
Y0[which(WH2data$y00_turn_nhisp==999999 | WH2data$nhvap==0 | WH2data$nhvap==999999)]<-NA
    
print("Percent Overrun")  
print(rbind(c('2006','2000'),round(c(length(which(Y1>1))/length(Y1),length(which(Y0>1))/length(Y0)),digits=4)))
                         
#Y1[which(Y1>1)] <- NA
#Y0[which(Y0>1)] <- NA

Y1 <- Y1[c(m1$index.treated,m1$index.control)]
Y0 <- Y0[c(m1$index.treated,m1$index.control)]

Tr <- WH2data$Tr == 1    
TrWH2<-Tr[c(m1$index.treated,m1$index.control)]


YWH2.1.Tr=Y1[TrWH2]
YWH2.1.Co=Y1[!TrWH2]
YWH2.0.Tr=Y0[TrWH2]
YWH2.0.Co=Y0[!TrWH2]


ddWH2.Tr <- mean(na.rm=T,YWH2.1.Tr) - mean(na.rm=T,YWH2.0.Tr)
ddWH2.Co <- mean(na.rm=T,YWH2.1.Co) - mean(na.rm=T,YWH2.0.Co)
ddWH2 <- ddWH2.Tr - ddWH2.Co 
print(ddWH2)

YY <- c(YWH2.1.Tr, YWH2.1.Co,YWH2.0.Tr, YWH2.0.Co)
time <- c(rep(1,length(c(YWH2.1.Tr, YWH2.1.Co))),rep(0,length(c(YWH2.0.Tr, YWH2.0.Co))))
TT <- c(rep(1,length(YWH2.1.Tr)),rep(0,length(YWH2.1.Co)),rep(1,length(YWH2.0.Tr)),rep(0,length(YWH2.0.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = YWH2.1.Tr - YWH2.0.Tr
DCo = YWH2.1.Co - YWH2.0.Co
D1 = YWH2.1.Tr - YWH2.1.Co  
D0 = YWH2.0.Tr - YWH2.0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

RESACT[[3]]["NonHispanic Turnout 2006-2000 Difference WH2","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[3]]["NonHispanic Turnout 2006-2000 Difference WH2","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]



######################################################
### (i) Diff-in-Diff results on Hispanic Registration
######################################################

#MATCHED 
    
YSt1 <- (Sdata$y06_reg_hisp/Sdata$hvap)
YSt0 <- (Sdata$y00_reg_hisp/Sdata$hvap)

indx <- which(Sdata$hvap==0 | Sdata$hvap==999999 | 
Sdata$y06_reg_hisp==999999 | Sdata$y00_reg_hisp==999999 )

YSt1[indx] <- NA
YSt0[indx] <- NA
  
print("Percent Overrun")  
print(rbind(c('2006','2000'),round(c(length(which(YSt1>1))/length(YSt1),length(which(YSt0>1))/length(YSt0)),digits=4)))

#YSt1[which(YSt1>1)] <- NA
#YSt0[which(YSt0>1)] <- NA

Y1m.Tr<-YSt1[m2$index.treated]
Y1m.Co<-YSt1[m2$index.control]
Y1m  <- c(Y1m.Tr,Y1m.Co)
Tr1m <- c(rep(TRUE,length(Y1m.Tr)),rep(FALSE,length(Y1m.Co)))

Y0m.Tr<-YSt0[m2$index.treated]
Y0m.Co<-YSt0[m2$index.control]
Y0m  <- c(Y0m.Tr,Y0m.Co)
Tr1m <- c(rep(TRUE,length(Y0m.Tr)),rep(FALSE,length(Y0m.Co)))

mddTr <- mean(na.rm=T,Y1m.Tr) - mean(na.rm=T,Y0m.Tr)
mddCo <- mean(na.rm=T,Y1m.Co) - mean(na.rm=T,Y0m.Co)
mdd <- mddTr - mddCo
print(mdd)

YY <- c(Y1m.Tr, Y1m.Co,Y0m.Tr, Y0m.Co)
time <- c(rep(1,length(c(Y1m.Tr, Y1m.Co))),rep(0,length(c(Y0m.Tr, Y0m.Co))))
TT <- c(rep(1,length(Y1m.Tr)),rep(0,length(Y1m.Co)),rep(1,length(Y0m.Tr)),rep(0,length(Y0m.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = Y1m.Tr - Y0m.Tr
DCo = Y1m.Co - Y0m.Co
D1 = Y1m.Tr - Y1m.Co
D0 = Y0m.Tr - Y0m.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)


RESACT[[3]]["Hispanic Registration 2006-2000 Difference AM","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[3]]["Hispanic Registration 2006-2000 Difference AM","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]




######################################################
### (ii) Diff-in-Diff results on Hispanic Turnout
######################################################

#MATCHED

YSt1 <- (Sdata$y06_turn_hisp/Sdata$hvap)
YSt0 <- (Sdata$y00_turn_hisp/Sdata$hvap)

indx <- which(Sdata$hvap==0 | Sdata$hvap==999999 | 
Sdata$y06_turn_hisp==999999 | Sdata$y00_turn_hisp==999999 )

YSt1[indx] <- NA
YSt0[indx] <- NA
             
print("Percent Overrun")  
print(rbind(c('2006','2000'),round(c(length(which(YSt1>1))/length(YSt1),length(which(YSt0>1))/length(YSt0)),digits=4)))

#YSt1[which(YSt1>1)] <- NA
#YSt0[which(YSt0>1)] <- NA

Y1m.Tr<-YSt1[m2$index.treated]
Y1m.Co<-YSt1[m2$index.control]
Y1m  <- c(Y1m.Tr,Y1m.Co)
Tr1m <- c(rep(TRUE,length(Y1m.Tr)),rep(FALSE,length(Y1m.Co)))

Y0m.Tr<-YSt0[m2$index.treated]
Y0m.Co<-YSt0[m2$index.control]
Y0m  <- c(Y0m.Tr,Y0m.Co)
Tr1m <- c(rep(TRUE,length(Y0m.Tr)),rep(FALSE,length(Y0m.Co)))

mddTr <- mean(na.rm=T,Y1m.Tr) - mean(na.rm=T,Y0m.Tr)
mddCo <- mean(na.rm=T,Y1m.Co) - mean(na.rm=T,Y0m.Co)
mdd <- mddTr - mddCo
print(mdd)

YY <- c(Y1m.Tr, Y1m.Co,Y0m.Tr, Y0m.Co)
time <- c(rep(1,length(c(Y1m.Tr, Y1m.Co))),rep(0,length(c(Y0m.Tr, Y0m.Co))))
TT <- c(rep(1,length(Y1m.Tr)),rep(0,length(Y1m.Co)),rep(1,length(Y0m.Tr)),rep(0,length(Y0m.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = Y1m.Tr - Y0m.Tr
DCo = Y1m.Co - Y0m.Co
D1 = Y1m.Tr - Y1m.Co
D0 = Y0m.Tr - Y0m.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)


RESACT[[3]]["Hispanic Turnout 2006-2000 Difference AM","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[3]]["Hispanic Turnout 2006-2000 Difference AM","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]


######################################################
### (iii) Diff-in-Diff results on NonHispanic Registration
######################################################

#MATCHED  
   

YSt1 <- (Sdata$y06_reg_nhisp/Sdata$nhvap)
YSt0 <- (Sdata$y00_reg_nhisp/Sdata$nhvap)

indx <- which(Sdata$nhvap==0 | Sdata$nhvap==999999 |
Sdata$y06_reg_nhisp==999999 | Sdata$y00_reg_nhisp==999999 )

YSt1[indx] <- NA
YSt0[indx] <- NA
      
print("Percent Overrun")  
print(rbind(c('2006','2000'),round(c(length(which(YSt1>1))/length(YSt1),length(which(YSt0>1))/length(YSt0)),digits=4)))

#YSt1[which(YSt1>1)] <- NA
#YSt0[which(YSt0>1)] <- NA

Y1m.Tr<-YSt1[m2$index.treated]
Y1m.Co<-YSt1[m2$index.control]
Y1m  <- c(Y1m.Tr,Y1m.Co)
Tr1m <- c(rep(TRUE,length(Y1m.Tr)),rep(FALSE,length(Y1m.Co)))

Y0m.Tr<-YSt0[m2$index.treated]
Y0m.Co<-YSt0[m2$index.control]
Y0m  <- c(Y0m.Tr,Y0m.Co)
Tr1m <- c(rep(TRUE,length(Y0m.Tr)),rep(FALSE,length(Y0m.Co)))

mddTr <- mean(na.rm=T,Y1m.Tr) - mean(na.rm=T,Y0m.Tr)
mddCo <- mean(na.rm=T,Y1m.Co) - mean(na.rm=T,Y0m.Co)
mdd <- mddTr - mddCo
print(mdd)

YY <- c(Y1m.Tr, Y1m.Co,Y0m.Tr, Y0m.Co)
time <- c(rep(1,length(c(Y1m.Tr, Y1m.Co))),rep(0,length(c(Y0m.Tr, Y0m.Co))))
TT <- c(rep(1,length(Y1m.Tr)),rep(0,length(Y1m.Co)),rep(1,length(Y0m.Tr)),rep(0,length(Y0m.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = Y1m.Tr - Y0m.Tr
DCo = Y1m.Co - Y0m.Co
D1 = Y1m.Tr - Y1m.Co
D0 = Y0m.Tr - Y0m.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)


RESACT[[3]]["NonHispanic Registration 2006-2000 Difference AM","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[3]]["NonHispanic Registration 2006-2000 Difference AM","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]


######################################################
### (iv) Diff-in-Diff results on NonHispanic Turnout
######################################################

#MATCHED

YSt1 <- (Sdata$y06_turn_nhisp/Sdata$nhvap)
YSt0 <- (Sdata$y00_turn_nhisp/Sdata$nhvap)

indx <- which(Sdata$nhvap==0 | Sdata$nhvap==999999 |
Sdata$y06_turn_nhisp==999999 | Sdata$y00_turn_nhisp==999999 )

YSt1[indx] <- NA
YSt0[indx] <- NA

print("Percent Overrun")  
print(rbind(c('2006','2000'),round(c(length(which(YSt1>1))/length(YSt1),length(which(YSt0>1))/length(YSt0)),digits=4)))

#YSt1[which(YSt1>1)] <- NA
#YSt0[which(YSt0>1)] <- NA

Y1m.Tr<-YSt1[m2$index.treated]
Y1m.Co<-YSt1[m2$index.control]
Y1m  <- c(Y1m.Tr,Y1m.Co)
Tr1m <- c(rep(TRUE,length(Y1m.Tr)),rep(FALSE,length(Y1m.Co)))

Y0m.Tr<-YSt0[m2$index.treated]
Y0m.Co<-YSt0[m2$index.control]
Y0m  <- c(Y0m.Tr,Y0m.Co)
Tr1m <- c(rep(TRUE,length(Y0m.Tr)),rep(FALSE,length(Y0m.Co)))

mddTr <- mean(na.rm=T,Y1m.Tr) - mean(na.rm=T,Y0m.Tr)
mddCo <- mean(na.rm=T,Y1m.Co) - mean(na.rm=T,Y0m.Co)
mdd <- mddTr - mddCo
print(mdd)

YY <- c(Y1m.Tr, Y1m.Co,Y0m.Tr, Y0m.Co)
time <- c(rep(1,length(c(Y1m.Tr, Y1m.Co))),rep(0,length(c(Y0m.Tr, Y0m.Co))))
TT <- c(rep(1,length(Y1m.Tr)),rep(0,length(Y1m.Co)),rep(1,length(Y0m.Tr)),rep(0,length(Y0m.Co)))
lmout = summarize.r(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = Y1m.Tr - Y0m.Tr
DCo = Y1m.Co - Y0m.Co
D1 = Y1m.Tr - Y1m.Co
D0 = Y0m.Tr - Y0m.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)


RESACT[[3]]["NonHispanic Turnout 2006-2000 Difference AM","Mean Diff"] = lmout$coefficients["I(time * TT)","Estimate"]
RESACT[[3]]["NonHispanic Turnout 2006-2000 Difference AM","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]

               
print(RESACT)



################## LATEX TABLES ##################
library(xtable)
library(stringr)      

baseline=cbind(RESACT[[1]][1:4,],RESACT[[2]][1:4,])
matched=cbind(RESACT[[1]][5:8,],RESACT[[2]][5:8,])

rownames(matched)=rownames(baseline)=c('Hispanic Turnout','Hispanic Registration','Non Hispanic Turnout','Non Hispanic Registration')     
  
print('--------------------')
print('Baseline')
print('--------------------')  
xtable(baseline,digits=c(1,3,2,3,2)) 

print('--------------------')
print('Matched')
print('--------------------')  
xtable(matched,digits=c(1,3,2,3,2))
                                              
################## LATEX TABLES ##################